package coupon_distribution

import (
	sqlc "bp/db/sqlc"
	"bp/internal/render"
	sharedVld "bp/internal/rpcserver/shared/vld"
	"bp/internal/service/entity/coupon"
	"bp/internal/util"
	couponDistributionGrpc "bp/rpc/gen/webapi/coupon_distribution"
	"context"
)

// VldCreateCouponDistributionReq 验证新建优惠券投放请求
func (s *CouponDistributionWebApiImp) VldCreateCouponDistributionReq(ctx context.Context, q *sqlc.Queries, req *couponDistributionGrpc.CreateCouponDistributionReq) (ptr *coupon.CreateCouponDistributionParam, needExecuteImmediately bool, err error) {
	slog := util.NewContextLogger(ctx, s.slog)

	if req.CouponId < 0 {
		err = render.InvalidArgumentRange("coupon_id")
		return
	}
	if !sharedVld.IsIssuer(req.Issuer) {
		err = render.InvalidArgumentValue("issuer")
		return
	}
	if !sharedVld.IsCouponDistributeScene(req.DistributeScene) {
		err = render.InvalidArgumentValue("distribute_scene")
		return
	}
	if req.Cnt < 0 {
		err = render.InvalidArgumentRange("cnt")
		return
	}

	ptr = &coupon.CreateCouponDistributionParam{
		CreateCouponDistributionParamsPtr: &sqlc.CreateCouponDistributionParams{
			CouponID:        req.CouponId,
			Issuer:          req.Issuer,
			DistributeScene: req.DistributeScene,
			BatchNo:         s.entityHub.CouponService.GenerateCouponDistributionBatchNo(),
			Cnt:             req.Cnt,
			Memo:            sqlc.NSFromString(req.Memo),
		},
	}
	needExecuteImmediately = req.NeedExecuteImmediately
	slog.Infof("coupon_distribution_imp/cmd_vld.VldCreateCouponDistributionReq ok")
	return
}

// VldUpdateCouponDistributionReq 验证修改优惠券投放请求
func (s *CouponDistributionWebApiImp) VldUpdateCouponDistributionReq(ctx context.Context, q *sqlc.Queries, req *couponDistributionGrpc.UpdateCouponDistributionReq) (ptr *coupon.UpdateCouponDistributionParam, err error) {
	slog := util.NewContextLogger(ctx, s.slog)

	if req.Id < 0 {
		err = render.InvalidArgumentRange("id")
		return
	}
	if !sharedVld.IsIssuer(req.Issuer) {
		err = render.InvalidArgumentValue("issuer")
		return
	}
	if !sharedVld.IsCouponDistributeScene(req.DistributeScene) {
		err = render.InvalidArgumentValue("distribute_scene")
		return
	}
	if req.Cnt < 0 {
		err = render.InvalidArgumentRange("cnt")
		return
	}

	ptr = &coupon.UpdateCouponDistributionParam{
		DistributionID:  req.Id,
		Issuer:          req.Issuer,
		DistributeScene: req.DistributeScene,
		Cnt:             req.Cnt,
		Memo:            req.Memo,
	}

	slog.Infof("coupon_distribution_imp/cmd_vld.VldUpdateCouponDistributionReq ok")
	return
}

// VldExecuteCouponDistributionReq 验证执行优惠券投放请求
func (s *CouponDistributionWebApiImp) VldExecuteCouponDistributionReq(ctx context.Context, q *sqlc.Queries, req *couponDistributionGrpc.ExecuteCouponDistributionReq) (ptr *coupon.ExecuteCouponDistributionParam, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	if req.Id < 0 {
		err = render.InvalidArgumentRange("id")
		return
	}
	ptr = &coupon.ExecuteCouponDistributionParam{
		DistributionID: req.Id,
	}
	slog.Infof("coupon_distribution_imp/cmd_vld.VldExecuteCouponDistributionReq ok")
	return
}
